package com.dgut.service.impl;

import com.dgut.dao.CustomerDao;
import com.dgut.domain.Customer;
import com.dgut.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Override
    public List<Customer> findAllCustomer() {
        return customerDao.findAllCustomer();
    }

    @Override
    public List<Customer> findAllByPage(int startIndex, int perPageSize) {
        return customerDao.findAllByPage(startIndex,perPageSize);
    }

    @Override
    public int customerNum() {
        return customerDao.customerNum();
    }

    @Override
    public void deleteCustomer(int id) {
        customerDao.deleteCustomer(id);
    }

    @Override
    @Transactional
    public void insertCustomer(Customer customer)  {
        System.out.println("insertCustomerService");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//设置日期格式
        System.out.println("时间"+df.format(new Date()));// new Date()为获取当前系统时间
        customer.setCreatetime(df.format(new Date()));
       customerDao.insertCustomer(customer);
        System.out.println("测试事务");
//       int i =1/0;

    }

    @Override
    public Customer getCustomerById(int id) {
        return customerDao.getCustomerById(id);
    }

    @Override
    public void updateCustomer(Customer customer) {
        customerDao.updateCustomer(customer);
    }

    @Override
    public List<Customer> selectCustomerByPara(Customer customer) {
        String name = customer.getName();
        name = "%"+name+"%";
        System.out.println(name);
        customer.setName(name);
        return customerDao.selectCustomerByPara(customer);
    }



}
